package com.monika.main.system.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Author whh
 * Date 2023/06/13/ 20:32
 * <p></p>
 */
public class Demo {


    public static void main(String[] args) {
        try {
            jpsPid();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static int jpsPid() throws IOException {
        int pid = -1;
        Process process = null;

        BufferedReader reader = null;

        process = Runtime.getRuntime().exec("jps -l");

        reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line;

        while ((line = reader.readLine()) != null) {

            String[] strings = line.split("\\s{1,}");

            if (strings.length < 2)
            {
                continue;
            }


            pid = Integer.parseInt(strings[0]);


        }

        reader.close();

        process.destroy();

        return pid;

    }
}
